<?php


namespace App\Http\Controllers\WebApi;


use App\Models\ImFilesStorage;
use Illuminate\Http\Request;

class FilesController
{
    /**
     * 上传文件列表
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function fileList(Request $req)
    {
        $source = $req->get('source');
        if (!in_array($source, [1, 2])) {
            return errBack('请选择正确来源');
        }
        list($offset, $limit, $page, $pagesize) = ImUserFriend::pageProcess();
        $query = new ImFilesStorage();
        if ($source == 1) {
            $query = $query->with(['user'])->where('source', 1);
        } else {
            $query = $query->with(['account'])->where('source', 2);
        }
        $total = $query->count();
        $data = $query->offset($offset)->limit($limit)->orderBy('id','desc')->get();
        foreach ($data as $val) {
            if ($source == 2) {
                $val->user = $val->account;
                unset($val->account);
            }
        }
        return okBack($data, [
            'total' => $total,
            'page' => $page,
            'pagesize' => $pagesize,
        ]);
    }
}
